﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web;

namespace LunwenYun.Tool.Configration
{
    public class SiteResource
    {

        static Resource resource = null;
        static SiteResource()
        {
            XElement xe = XElement.Load(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"/content/config/sitesetting.xml");
            resource = new Resource()
            {
                Host = new Resource_Host()
                {
                    Domain = xe.Element("host").Element("domain").Value,
                    SiteName = xe.Element("host").Element("name").Value
                },
                Setting = xe.Element("host").Element("setting").Elements()
                        .ToDictionary(
                            xn => { return xn.Name.ToString(); },
                            xn => { return xn.Value; }),
                Company = xe.Element("host").Element("company").Elements()
                    .ToDictionary(
                        xn => { return xn.Name.ToString(); },
                        xn => { return xn.Value; }),
                seo = new SEOSettings()
                {
                    Default = new SEOSettingItem() { 
                       title =  xe.Element("seo").Element("default").Attribute("title").Value,
                       keywords = xe.Element("seo").Element("default").Attribute("keyword").Value,
                       description = xe.Element("seo").Element("default").Attribute("description").Value
                    },
                    PageSet=xe.Element("seo").Elements("item")
                    .ToDictionary(
                        xn=>{ 
                            return xn.Attribute("name").Value; 
                        },
                        xn=>{
                            SEOSettingItem item = new SEOSettingItem();
                            item.title = xn.Attribute("title").Value;
                            item.keywords = xn.Attribute("keyword").Value;
                            item.description = xn.Attribute("description").Value;
                            return item;
                        })
                }
            };
        }

        #region 字段
        public static string Domain
        {
            get
            {
                return resource.Host.Domain;
            }
        }
        public static string SiteName
        {
            get
            {
                return resource.Host.SiteName;
            }
        }
        public static string CompanyAddress
        {
            get
            {
                return resource.Company["address"];
            }
        }
        public static string CompanyPhone
        {
            get
            {
                return resource.Company["phone"];
            }
        }
        public static string Logo
        {
            get
            {
                return resource.Setting["logo"];
            }
        }
        public static string[] AdminQQ
        {
            get
            {
                string qq = resource.Setting["adminqq"];
                if (!String.IsNullOrEmpty(qq))
                {
                    return qq.Split(',');
                }
                return new string[] { };
            }
        }
        static Dictionary<string, Dictionary<string, string>> result = null;
        public static Dictionary<string, Dictionary<string, string>> SeoSettings 
        {
            get {
                if (result == null || !result.Any())
                {
                    result = new Dictionary<string, Dictionary<string, string>>();
                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    dict.Add("title", resource.seo.Default.title);
                    dict.Add("keywords", resource.seo.Default.keywords);
                    dict.Add("description", resource.seo.Default.description);
                    result.Add("default", dict);
                    foreach (var item in resource.seo.PageSet)
                    {
                        dict = new Dictionary<string, string>();
                        dict.Add("title", item.Value.title);
                        dict.Add("keywords", item.Value.keywords);
                        dict.Add("description", item.Value.description);
                        result.Add(item.Key, dict);
                    }
                }
                return result;
            }
        }
        #endregion

        #region
        private class Resource
        {
            public Resource_Host Host { get; set; }
            public Dictionary<string, string> Company { get; set; }
            public Dictionary<string, string> Setting { get; set; }
            public SEOSettings seo{get;set;}
        }
        private class Resource_Host
        {
            /// <summary>
            /// 站点域名 duo500.com
            /// </summary>
            public string Domain { get; set; }
            /// <summary>
            /// 站点名
            /// </summary>
            public string SiteName { get; set; }
        }
        private class SEOSettings
        {
            public SEOSettingItem Default { get; set; }
            public Dictionary<string, SEOSettingItem> PageSet { get; set; }
        }
        private class SEOSettingItem
        {
            public string title { get; set; }
            public string keywords { get; set; }
            public string description { get; set; }
        }
        #endregion
    }
}
